//
//  HRSettingViewController.m
//  Hear
//
//  Created by mac on 2017/3/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRSettingViewController.h"
#import "HRBaseTableView.h"
#import "HRSettingTableViewModel.h"
#import "HearNavigationController.h"
#import "HRPhoneLoginViewController.h"
//#import "MyPlayTool.h"

@interface HRSettingViewController ()
@property (nonatomic, strong)HRBaseTableView *tableView;
@property (nonatomic, strong)HRSettingTableViewModel *tableViewModel;
@property (nonatomic, strong)UIView *footView;

@end

@implementation HRSettingViewController

-(HRSettingTableViewModel *)tableViewModel
{
    if (_tableViewModel == nil)
    {
        _tableViewModel = [HRSettingTableViewModel new];
    }
    return _tableViewModel;
}

-(UIView *)footView
{
    if (_footView == nil){
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kFullWidth, 80)];
        UIButton *signtOutBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 40, kFullWidth, 40)];
        [signtOutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        [signtOutBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        signtOutBtn.titleLabel.font = CustomUIFont(15);
        [signtOutBtn addTarget:self action:@selector(signtOutBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_footView addSubview:signtOutBtn];
        
        
        UILabel *versionLabel = [UILabel new];
        versionLabel.font = CustomUIFont(12);
        versionLabel.textColor = FirstTextColor;
        [_footView  addSubview:versionLabel];
        versionLabel.frame = CGRectMake(0, signtOutBtn.bottom-10, _footView.width, 20);
        versionLabel.textAlignment = 1;
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        versionLabel.text = [NSString stringWithFormat:@"版本号：%@",[infoDictionary objectForKey:@"CFBundleShortVersionString"]];
    }
    return _footView;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"设置";

    
    _tableView = [HRBaseTableView new];
    [self.view addSubview:self.tableView];

    self.tableView.frame = self.view.frame;
    self.tableView.tableFooterView = self.footView;
    self.tableView.rowHeight = 50;
    [self.tableViewModel handleWithTable:self.tableView];
    
}



#pragma -mark- 退出登录
- (void)signtOutBtnClick
{
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"cacheTime"];

    [self showHudInView:self.view hint:@"退出登录"];
    
    [self clearData];
//    [[MyPlayTool sharePlayTool] stop];

    WeakSelf
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       [[EMClient sharedClient]logout:YES completion:^(EMError *aError) {
           dispatch_async(dispatch_get_main_queue(), ^{
               [weakSelf hideHud];
               
               if (aError != nil) {
                   [weakSelf showHint:aError.errorDescription];
               }
               else{
                   
                   [weakSelf.navigationController removeFromParentViewController];
                   UIWindow *window = [UIApplication sharedApplication].keyWindow;
                   [window removeAllSubviews];
                   window = nil;
                   
                   [UIApplication sharedApplication].keyWindow.rootViewController = [[HearNavigationController alloc] initWithRootViewController:[HRPhoneLoginViewController new]];
//
                   [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
               }
           });
       }];
       
    });
    
}

- (void)clearData{
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"iconName"];
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"iconColor"];

    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"sex"];

    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"nickName"];
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"userid"];
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"userInfo"];
    [[NSUserDefaults standardUserDefaults] setURL:nil forKey:@"SoundUrl"];

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.hidesBottomBarWhenPushed = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
